<template>
    <input-select :id="id"  :options="optionss" :styles="styles"  :sign="sign" :time="time"  :valsql="valsql" :index="index" isChange="Y"  :data="data" :exkey="exkey" @input="changeData"></input-select>
</template>
<script>
import InputSelect  from '../input/input-select'
export default {
    props:['id','styles','value','data','index','appid','valsql','sign','time','exkey','selectData','options'],
    name: "item-select",
    components: {
        InputSelect
    },
     data(){
        return{
            optionss:[],
            sz_depart:"",
            val:"",
            type:"",
            type2:"",
            dataSource:"",
            dataField:"item",
            oldVal:"",
            names:""
        }
    },
    created(){
        
        this.type=this.exkey.type;
        this.type2=this.exkey.type2;
       
        try{
            this.dataSource=this.exkey.dataSource;
        }catch(e){}
        /*if(!this.valsql){
            this.$post("bygex","byg/funeral/getFuneralSfItem",{type2:this.type}).then(data=>{
                this.sfItemData=[];
                for(let i=0;i<data.item.length;i++){
                    this.sfItemData.push(data.item[i]);
                }
            });
        }*/
        this.sz_depart=this.data["szInfo"].sz_depart;
        
        this.loadItem(true);

        let ids=this.id.split(".");
        let item_name=(this.data[ids[0]][ids[1]])
        for(let i=0;i<this.data["item"].length;i++){
            let szi_ex1=this.data["item"][i]["szi_ex1"];
            if(szi_ex1==item_name){
                this.oldVal=this.data["item"][i]["szi_ex8"];
                break;
            }
        }
    },
    methods:{
        changeData(data){
            console.log("selectdata=1================="+data)
            let item=JSON.parse(data);
            this.val=item.val;
            let sz_depart_=this.data["szInfo"].sz_depart;
            if(this.sz_depart!=sz_depart_){
                this.loadItem(false);
            }else{
                this.addItem(false);
            }
            
        },
        loadItem(isFirst){
             this.optionss=[];
             this.$post("bygex","byg/funeral/getFuneralSfItem",{depart:this.sz_depart,type2:this.type,type4:this.type2}).then(data=>{
                this.sfItemData=[];
                this.optionss.push({
                        optname:"",
                        optvalue:""
                    })
                for(let i=0;i<data.item.length;i++){
                    this.sfItemData.push(data.item[i]);
                    this.optionss.push({
                        optname:data.item[i].szi_ex1,
                        optvalue:data.item[i].szi_ex1
                    })
                }
                if(this.options){
                    for(let i=0;i<this.options.length;i++){
                        this.optionss.push(this.options[i]);
                    }
                }
                
                if(this.val)
                    this.addItem(isFirst);
            });
        },
        addItem(isFirst){
           
            let isOk=true;
            for(let i=0;i<this.data["item"].length;i++){
                let szi_ex1=this.data["item"][i]["szi_ex1"];
                if(szi_ex1==this.val){
                    isOk=false;
                    break;
                }
            }
            if(isOk){
                let index=-1;
                 for(let i=0;i<this.data["item"].length;i++){
                    let szi_ex7=this.data["item"][i]["szi_ex7"];
                    let szi_ex9=this.data["item"][i]["szi_ex9"];
                    if(szi_ex7==this.type&&this.type){
                        index=i;
                        break;
                    }
                    if(this.type2){
                        if(szi_ex9==this.type2){
                            index=i;
                            break;
                        }
                    }
                    
                }
                if(index!=-1){
                    this.$delete(this.data["item"],index);
                }
                let row=null;
                for(let i=0;i<this.sfItemData.length;i++){
                     let szi_ex1=this.sfItemData[i]["szi_ex1"];
                    if(szi_ex1==this.val){
                       row=this.sfItemData[i]
                    }
                }
                if(row){
                    let datas={}
                    datas.addType="string";
                    datas.itemType=this.type;
                    datas.nameType=this.val;
                    datas.val=this.val;
                    datas.hour="1";
                    datas.day="1";
                   
                    if(this.oldVal)
                        this.delRelItem(this.oldVal)
                    this.$emit("funeralItemSelectEvent",JSON.stringify(datas))
                    this.oldVal=row.szi_ex8;
                   // this.$set(this.data["item"],this.data["item"].length,row)
                   // this.addRelItem(row,1,1)
                }else if(this.val==""){
                    if(this.oldVal)
                        this.delRelItem(this.oldVal)
                    this.oldVal="";
                }
            }
        },
        delRelItem(sf_id){//添加相搭配项目，并且有些搭配项目是要根据添加或者小时计算数量的，也要更新这些相关联项目的数量
            console.log("addRelItem======================1"+"="+sf_id+"=="+this.names);

           /* if(this.names){
                let names=this.names.split(",");
                for(let i=0;i<names.length;i++){
                    let n=names[i];
                    //该方法后面还会在调用本方法
                    let index=-1;
                    for(let h=0;h<this.data["item"].length;h++){
                        let szi_ex1=this.data["item"][h].szi_ex1;
                        if(n==szi_ex1){
                            index=h;
                        }
                    }
                    if(index>=0){
                        this.$delete(this.data["item"],index)
                    }
                    
                }
            }else{*/
            
                //添加搭配项目
                this.$post("bygex","/byg/funeral/getSfItemDp",{sf_id:sf_id}).then(data=>{
                    console.log("delRelItem===========dp")
                    //添加相关联的服务项目
                    if(data.name){
                        this.names=data.name;
                        let names=data.name.split(",");
                        for(let i=0;i<names.length;i++){
                            let n=names[i];
                            //该方法后面还会在调用本方法
                            for(let h=0;h<this.data["item"].length;h++){
                                let szi_ex1=this.data["item"][h].szi_ex1;
                                if(n==szi_ex1){
                                    this.$delete(this.data["item"],h);
                                    break;
                                }
                            }
                            
                            
                        }
                    }
                

                // this.setJm();
                    //this.changeAmt();
                })
          //  }
            
        },
    }
}
</script>